package com.csust.cg.mongodb.contorller;

import com.csust.cg.mongodb.domain.HttpStatus;
import com.csust.cg.mongodb.domain.Result;
import com.csust.cg.mongodb.service.ConnectGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenguang
 */
@RestController
@RequestMapping("/api/group")
@ResponseBody
public class ConnectGroupController {

    @Autowired
    private ConnectGroupService connectGroupService;

    @PostMapping("/addGroup")
    public Result addGroup(@RequestParam("token") String token, @RequestParam("name") String name) {
        if (StringUtils.isBlank(token)) {
            return new Result("Token不能为空！", HttpStatus.BAD_REQUEST);
        }
        if (StringUtils.isBlank(name)) {
            return new Result("分组名不能为空！", HttpStatus.BAD_REQUEST);
        }
        return connectGroupService.addGroup(token, name);
    }

    @PostMapping("/removeGroup")
    public Result removeGroup(@RequestParam("id") Integer id) {
        if (id == null) {
            return new Result("分组ID不能为空！", HttpStatus.BAD_REQUEST);
        }
        return connectGroupService.removeGroup(id);
    }

    @PostMapping("/editGroup")
    public Result editGroup(@RequestParam("id") Integer id, @RequestParam("name") String name) {
        if (StringUtils.isBlank(name)) {
            return new Result("分组名不能为空！", HttpStatus.BAD_REQUEST);
        }
        if (id == null) {
            return new Result("分组ID不能为空！", HttpStatus.BAD_REQUEST);
        }
        return connectGroupService.editGroup(id, name);
    }

    @GetMapping("/getAllGroup")
    public Result getAllGroup(@RequestParam("token") String token) {
        if (StringUtils.isBlank(token)) {
            return new Result("Token不能为空！", HttpStatus.BAD_REQUEST);
        }
        return connectGroupService.getAllGroup(token);
    }
}
